/**
 * Project:   dtu.fmes.googleloon.ui
 * File:      InfoWindow.java
 * License: 
 *            This file is licensed under GNU General Public License version 3
 *            http://www.gnu.org/licenses/gpl-3.0.txt
 *
 * Copyright: Bartosz Cichecki [ cichecki.bartosz@gmail.com ]
 * Date:      Dec 6, 2013
 */

package dtu.fmes.loon.ui;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import org.apache.commons.lang3.StringUtils;

import dtu.fmes.loon.model.Balloon;

/**
 * @author Bartosz Cichecki
 * 
 */
public class InfoWindow extends JFrame {

	private static final long serialVersionUID = -1182080197598348896L;

	private JPanel contentPane = new JPanel();

	private final JLabel lblBalloonId = new JLabel("Balloon ID:");

	private final JTextField txtFldBalloonId = new JTextField();

	private final JLabel lblLattitudemeters = new JLabel("Lattitude (meters):");

	private final JLabel lblLongitudemeters = new JLabel("Longitude (meters):");

	private final JLabel lblAltitudemeters = new JLabel("Altitude (meters):");

	private final JLabel lblTargetAltitudemeters = new JLabel("Target altitude (meters):");

	private final JLabel lblBatteryCapacitywh = new JLabel("Battery capacity (Wh):");

	private final JTextField txtFldLatitude = new JTextField();

	private final JTextField txtFldLongitude = new JTextField();

	private final JTextField txtFldAltitude = new JTextField();

	private final JTextField txtFldTargetAltitude = new JTextField();

	private final JTextField txtFldBatteryCapacity = new JTextField();

	private final JLabel lblIsAlive = new JLabel("Is alive?");

	private final JTextField txtFldIsAlive = new JTextField();

	private final JLabel lblIsResurectable = new JLabel("Is resurectable?");

	private final JTextField txtFldResurectable = new JTextField();

	public InfoWindow() {
		setAlwaysOnTop(true);
		setType(Type.UTILITY);
		setTitle("Info");
		setResizable(false);
		setBounds(100, 100, 248, 244);
		setContentPane(contentPane);

		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);

		lblBalloonId.setBounds(10, 11, 100, 14);
		contentPane.add(lblBalloonId);

		lblLattitudemeters.setBounds(10, 36, 100, 14);
		contentPane.add(lblLattitudemeters);

		lblLongitudemeters.setBounds(10, 61, 100, 14);
		contentPane.add(lblLongitudemeters);

		lblAltitudemeters.setBounds(10, 86, 100, 14);
		contentPane.add(lblAltitudemeters);

		lblTargetAltitudemeters.setBounds(10, 111, 126, 14);
		contentPane.add(lblTargetAltitudemeters);

		lblBatteryCapacitywh.setBounds(10, 136, 110, 14);
		contentPane.add(lblBatteryCapacitywh);

		lblIsAlive.setBounds(10, 161, 73, 14);
		contentPane.add(lblIsAlive);

		lblIsResurectable.setBounds(10, 186, 86, 14);
		contentPane.add(lblIsResurectable);

		txtFldBatteryCapacity.setEditable(false);
		txtFldBatteryCapacity.setBounds(146, 133, 86, 20);
		txtFldBatteryCapacity.setColumns(10);
		contentPane.add(txtFldBatteryCapacity);

		txtFldTargetAltitude.setEditable(false);
		txtFldTargetAltitude.setBounds(146, 108, 86, 20);
		txtFldTargetAltitude.setColumns(10);
		contentPane.add(txtFldTargetAltitude);

		txtFldAltitude.setEditable(false);
		txtFldAltitude.setBounds(146, 83, 86, 20);
		txtFldAltitude.setColumns(10);
		contentPane.add(txtFldAltitude);

		txtFldLongitude.setEditable(false);
		txtFldLongitude.setBounds(146, 58, 86, 20);
		txtFldLongitude.setColumns(10);
		contentPane.add(txtFldLongitude);

		txtFldLatitude.setEditable(false);
		txtFldLatitude.setBounds(146, 33, 86, 20);
		txtFldLatitude.setColumns(10);
		contentPane.add(txtFldLatitude);

		txtFldBalloonId.setEditable(false);
		txtFldBalloonId.setBounds(146, 8, 86, 20);
		txtFldBalloonId.setColumns(10);
		contentPane.add(txtFldBalloonId);

		txtFldIsAlive.setEditable(false);
		txtFldIsAlive.setBounds(146, 158, 86, 20);
		txtFldIsAlive.setColumns(10);
		contentPane.add(txtFldIsAlive);

		txtFldResurectable.setEditable(false);
		txtFldResurectable.setBounds(146, 183, 86, 20);
		txtFldResurectable.setColumns(10);
		contentPane.add(txtFldResurectable);
	}

	public void clearWindow() {
		txtFldAltitude.setText(StringUtils.EMPTY);
		txtFldBalloonId.setText(StringUtils.EMPTY);
		txtFldBatteryCapacity.setText(StringUtils.EMPTY);
		txtFldIsAlive.setText(StringUtils.EMPTY);
		txtFldLatitude.setText(StringUtils.EMPTY);
		txtFldLongitude.setText(StringUtils.EMPTY);
		txtFldResurectable.setText(StringUtils.EMPTY);
		txtFldTargetAltitude.setText(StringUtils.EMPTY);
	}

	public void updateWindow(Balloon b) {
		txtFldAltitude.setText(String.format("%.2f", b.getAltitude()));
		txtFldBalloonId.setText(Integer.toString(b.getId()));
		txtFldBatteryCapacity.setText(String.format("%.2f", b.getCurrentBatteryCapacity()));
		txtFldIsAlive.setText(b.isAlive() ? "Yes" : "No");
		txtFldLatitude.setText(String.format("%.2f", b.getLatitude()));
		txtFldLongitude.setText(String.format("%.2f", b.getLongitude()));
		txtFldResurectable.setText(b.isCanBeRevived() ? "Yes" : "No");
		txtFldTargetAltitude.setText(String.format("%.2f", b.getTargetAltitude()));
	}
}
